package com.astrofizzbizz.raspberrypicamera.server;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import com.astrofizzbizz.raspberrypicamera.shared.RaspberryPiException;

public class TimeLapseCreator 
{

	public static void main(String[] args) throws RaspberryPiException 
	{
		double dinterval = Double.parseDouble(args[0]);
		dinterval = dinterval * 1000;
		int interval = (int) dinterval;
		
		double dmaxTime = Double.parseDouble(args[1]);
		dmaxTime = dmaxTime * 1000;
		int maxTime = (int) dmaxTime;

		int maxImages = Integer.parseInt(args[2]);
		
		String imageDir = args[3];
		
		if (maxImages > 9999) maxImages = 9999;
		String[] response;
		boolean linux = true;
		boolean getInfo = true;
		boolean expDebug = false;
		boolean ignoreErrors = false;
		String command;
		int numImages = maxTime / interval;
		if (numImages > maxImages) maxTime = interval * (maxImages - 1);
		File imageDirFile = new File(imageDir);
		PrintWriter pw;
		try {pw = new PrintWriter(imageDirFile.getParent() + "/timeLapseCreator.log");} catch (FileNotFoundException e1) {throw new RaspberryPiException(e1);}
		try 
		{
			command = "rm " + imageDir + "/*.jpg";
			response = ServerUtilities.runExternalProcess(command, linux, getInfo, expDebug, ignoreErrors);
			if (response != null) {if (response.length > 0) {for (int ii = 0; ii < response.length; ++ii) pw.println(response[ii]);}}
		} catch (RaspberryPiException e1) {}

		try 
		{
			try {Thread.sleep(3000);} catch (InterruptedException e) {}
			command = "touch " + imageDirFile.getParent() + "/timeLapseInProgress";
			response = ServerUtilities.runExternalProcess(command, linux, getInfo, expDebug, ignoreErrors);
			if (response != null) {if (response.length > 0) {for (int ii = 0; ii < response.length; ++ii) pw.println(response[ii]);}}
			command = "raspistill -o " + imageDir + "/temp_%04d.jpg -tl " + Integer.toString(interval) + " -t " + Integer.toString(maxTime);
			response = ServerUtilities.runExternalProcess(command, linux, getInfo, expDebug, ignoreErrors);
			if (response != null) {if (response.length > 0) {for (int ii = 0; ii < response.length; ++ii) pw.println(response[ii]);}}
			command = "rm " + imageDirFile.getParent() + "/timeLapseInProgress";
			response = ServerUtilities.runExternalProcess(command, linux, getInfo, expDebug, ignoreErrors);
			if (response != null) {if (response.length > 0) {for (int ii = 0; ii < response.length; ++ii) pw.println(response[ii]);}}

/*			try {Thread.sleep(3000);} catch (InterruptedException e) {}
			command = "avconv -r 10 -i " + imageDir + "/temp_%04d.jpg -r 10 -vcodec libx264 -crf 20 -g 15 -vf crop=2592:1458,scale=1280:720 " + imageDir + "/timeLapse.mp4 > " + imageDir + "/avconv1.log 2> " + imageDir + "/avconv2.log";
			response = ServerUtilities.runExternalProcess(command, linux, getInfo, expDebug);
			if (response != null) {if (response.length > 0) {for (int ii = 0; ii < response.length; ++ii) pw.println(response[ii]);}}

			try {Thread.sleep(3000);} catch (InterruptedException e) {}
			command = "rm " + imageDir + "/*.jpg";
			response = ServerUtilities.runExternalProcess(command, linux, getInfo, expDebug);
			if (response != null) {if (response.length > 0) {for (int ii = 0; ii < response.length; ++ii) pw.println(response[ii]);}}
*/			pw.close();

		} catch (RaspberryPiException e) 
		{
			e.printStackTrace(pw);
			pw.close();
		} 
	}

}
